package com.example.webjwt.handler;

import com.example.webcommon.enums.ResponseCodeEnum;
import com.example.webcommon.utils.Response;
import com.example.webjwt.utils.ResultUtil;
import io.jsonwebtoken.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: 犬小哈
 * @url: www.quanxiaoha.com
 * @date: 2023-08-27 17:32
 * @description: 登录成功访问收保护的资源，但是权限不够
 **/
@Slf4j
@Component
public class RestAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.warn("登录成功访问收保护的资源，但是权限不够: ", accessDeniedException);
        // 预留，后面引入多角色时会用到
        try {
            ResultUtil.fail(response, Response.fail(ResponseCodeEnum.FORBIDDEN));
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}
